const { Model, DataTypes } = require('sequelize');
const sequelize = require('../db/db');
const User = require('./User');

class UserProfile extends Model { }
UserProfile.init(
  {
    id: {
      type: DataTypes.CHAR(18),
      allowNull: false,
      primaryKey: true,
      comment: '主键ID'
    },
    user_id: {
      type: DataTypes.CHAR(18),
      allowNull: false,
      unique: true,
      comment: 'user_id',
      references: {
        model: User,
        key: 'user_id'
      }
    },
    total_question:{
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '用户在系统中的累计答题'
    },
    total_score: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '用户在系统中的累计积分'
    },
    correct_rate: {
      type: DataTypes.DECIMAL(5, 2),
      defaultValue: 0.00,
      comment: '用户答题的历史正确率，精确到小数点后两位，以百分比形式呈现'
    },
    battle_win: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '用户在对战模式中的胜利次数'
    }
  },
  {
    sequelize,
    modelName: 'UserProfile',
    tableName: 'user_profile',
    timestamps: false,
    comment: '用户答题统计表'
  }
);

// 建立与User表的关联
UserProfile.belongsTo(User, {
  foreignKey: 'user_id',
  targetKey: 'user_id'
});

module.exports = UserProfile;